package com.mjh.zhujianju.controller.petition;

import com.livew.framework.base.model.SysUser;
import com.livew.framework.base.service.SysUserService;
import com.livew.framework.exception.XTBusinessException;
import com.livew.framework.pojo.AjaxResult;
import com.livew.framework.utils.XTCurrentUser;
import com.mjh.zhujianju.enums.BanersEnum;
import com.mjh.zhujianju.enums.SupervisionTypeEnum;
import com.mjh.zhujianju.model.supervise.SupervisionEO;
import com.mjh.zhujianju.model.supervise.SupervisionEstimateEO;
import com.mjh.zhujianju.service.set.BanersService;
import com.mjh.zhujianju.service.supervise.SupervisionService;
import com.mjh.zhujianju.util.ApiConstant;
import com.mjh.zhujianju.util.JsonResult;
import com.mjh.zhujianju.vo.base.SysUserVO;
import com.mjh.zhujianju.vo.party.BuildingMeetingVO;
import com.mjh.zhujianju.vo.set.BanersVO;
import com.mjh.zhujianju.vo.supervision.SupervisionEstimateVO;
import com.mjh.zhujianju.vo.supervision.SupervisionVO;
import io.swagger.annotations.*;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: Alex
 * @version: V1.0
 * @project: zhujianju
 * @package: com.mjh.zhujianju.controller.petition
 * @description: 信访
 * @date: 2018/8/28
 * @time: 上午10:17
 */
@RestController
@RequestMapping("/letter")
@Api("3、WX->信访->写信")
public class PetitionController {

	@Autowired
	private SupervisionService supervisionService;

	@Autowired
	private BanersService banersService;

	@Resource
	private SysUserService userService;

	@ApiOperation(value="信访->新增写信", notes="新增写信")
	@RequestMapping(value = "/addLetter", method = RequestMethod.POST)
	@ApiImplicitParam(name="督办实体类", value="SupervisionVO", required = true, dataType = "SupervisionVO")
	public ResponseEntity<JsonResult> addLetter (SupervisionVO supervisionVO){
		JsonResult result = new JsonResult();
		Long userId = XTCurrentUser.getCurrentUserId();
		supervisionVO.setUserId(userId);
		try {
			if(supervisionVO.getId() != null){
				supervisionService.updateInfo(supervisionVO);
				result.setResult("修改成功");
			}else {
				supervisionService.saveInfo(supervisionVO);
				result.setResult("新增成功");
			}
			result.setStatus(ApiConstant.MSG_SUCCESS);
			result.setResult(supervisionVO.getId());
		}catch (XTBusinessException e){
			result.setResult(e.getClass().getName() + ":" + e.getMessage());
			result.setStatus(ApiConstant.MSG_ERROR);
			e.printStackTrace();
		}
		return ResponseEntity.ok(result);
	}

	@ApiOperation(value="信访->信件详情", notes="根据信件主键查询信件详情")
	@RequestMapping(value = "/letterDetail/{id}", method = RequestMethod.GET)
	@ApiImplicitParam(name = "id", value = "信件Id", required = true, dataType = "Long", paramType = "path")
	public ResponseEntity<JsonResult> letterDetail (@PathVariable(value = "id")Long id){
		JsonResult result = new JsonResult();
		try {
			SupervisionVO supervisionVO = supervisionService.queryLetterDetail(id);
			result.setStatus(ApiConstant.MSG_SUCCESS);
			result.setResult(supervisionVO);
		}catch (XTBusinessException e){
			result.setResult(e.getClass().getName() + ":" + e.getMessage());
			result.setStatus(ApiConstant.MSG_ERROR);
			e.printStackTrace();
		}
		return ResponseEntity.ok(result);
	}

	@ApiOperation(value="信访->所有信访", notes="查询所有信访")
	@RequestMapping(value = "/letterAll", method = RequestMethod.GET)
	public ResponseEntity<JsonResult> letterAll (){
		JsonResult result = new JsonResult();
		try {
			Map<String,List> resultMap = new HashMap<String,List>();
			List<SupervisionVO> supervisionVOs = supervisionService.queryLetterAll();

			//查询轮播图
			//轮播图
			List<BanersVO> banersEOList = banersService.queryBanersList(BanersEnum.BANERS_SITE_VISIT.getCode());
			resultMap.put("content",supervisionVOs);
			resultMap.put("baner",banersEOList);
			result.setStatus(ApiConstant.MSG_SUCCESS);
			result.setResult(resultMap);
		}catch (XTBusinessException e){
			result.setResult(e.getClass().getName() + ":" + e.getMessage());
			result.setStatus(ApiConstant.MSG_ERROR);
			e.printStackTrace();
		}
		return ResponseEntity.ok(result);
	}

	@ApiOperation(value="信访->评价", notes="对信访处理结果进行评价")
	@RequestMapping(value = "/letterEvaluate", method = RequestMethod.POST)
	@ApiImplicitParam(name = "评价实体类", value = "supervisionEstimateVO", required = true, dataType = "SupervisionEstimateVO")
	public ResponseEntity<JsonResult> letterEvaluate (SupervisionEstimateVO supervisionEstimateVO){
		JsonResult result = new JsonResult();
		try {
			supervisionEstimateVO.setUserId(XTCurrentUser.getCurrentUserId());
			supervisionService.saveEvaluateInfo(supervisionEstimateVO);
			result.setResult("评价成功");
			result.setStatus(ApiConstant.MSG_SUCCESS);
		}catch (XTBusinessException e){
			result.setResult(e.getClass().getName() + ":" + e.getMessage());
			result.setStatus(ApiConstant.MSG_ERROR);
			e.printStackTrace();
		}
		return ResponseEntity.ok(result);
	}

	@ApiOperation(value="信访->查看信件评价", notes="根据信件主键查询信件评价详情")
	@RequestMapping(value = "/letterEvaluateDetail/{id}", method = RequestMethod.GET)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "信访主键", required = true, dataType = "Long", paramType = "path"),
	})
	public ResponseEntity<JsonResult> letterEvaluateDetail (@PathVariable(value = "id")Long id){
		JsonResult result = new JsonResult();
		SupervisionEstimateVO supervisionEstimateVO = supervisionService.queryLetterEvaluateDetail(id,null);
		result.setResult(supervisionEstimateVO);
		return ResponseEntity.ok(result);
	}

	@ApiOperation(value="信访->用户中心", notes="根据信件主键查询用户信访次数")
	@RequestMapping(value = "/userLetterCount", method = RequestMethod.GET)
	public ResponseEntity<JsonResult> userLetterCount(){
		JsonResult result = new JsonResult();
		Long userId = XTCurrentUser.getCurrentUserId();

		Integer count = supervisionService.getUserLetterCount(userId);
		result.setResult(count);
		return ResponseEntity.ok(result);
	}

	@ApiOperation(value="信访->信访记录", notes="根据信件主键查询用户信访信息")
	@RequestMapping(value = "/userLetterInfo", method = RequestMethod.GET)
	public ResponseEntity<JsonResult> userLetterInfo(){
		JsonResult result = new JsonResult();
		Long userId = XTCurrentUser.getCurrentUserId();

		List<SupervisionVO> supervisionVOS = supervisionService.getUserLetterInfos(userId);
		result.setStatus(ApiConstant.MSG_SUCCESS);
		result.setResult(supervisionVOS);
		return ResponseEntity.ok(result);
	}

	@ApiOperation(value="信访->信访中心", notes="进行中的信访")
	@RequestMapping(value = "/lettering", method = RequestMethod.GET)
	public ResponseEntity<JsonResult> supervisionLetteringList (){
		JsonResult result = new JsonResult();
		List<SupervisionVO> supervisionVOS = supervisionService.getSupervisionLetteringList();
		result.setResult(supervisionVOS);
		result.setStatus(ApiConstant.MSG_SUCCESS);
		return ResponseEntity.ok(result);
	}

	@ApiOperation(value="信访->信访中心", notes="已完成的信访")
	@RequestMapping(value = "/lettered", method = RequestMethod.GET)
	public ResponseEntity<JsonResult> supervisionLetteredList (){
		JsonResult result = new JsonResult();
		List<SupervisionVO> supervisionVOS = supervisionService.getSupervisionLetteredList();
		result.setResult(supervisionVOS);
		result.setStatus(ApiConstant.MSG_SUCCESS);
		return ResponseEntity.ok(result);
	}

	@RequestMapping(value = "/letterDel/{id}", method = RequestMethod.GET)
	public ResponseEntity<JsonResult> letterDel (@PathVariable(value = "id")Long id){
		JsonResult result = new JsonResult();
		try {
			supervisionService.deleteSupervision(id);
			result.setStatus(ApiConstant.MSG_SUCCESS);
		}catch (Exception e){
			result.setResult(e.getClass().getName() + ":" + e.getMessage());
			result.setStatus(ApiConstant.MSG_ERROR);
			e.printStackTrace();
		}
		return ResponseEntity.ok(result);
	}


}
